package me.zhanshi123.timedworld;

import com.google.common.base.Charsets;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConfigManager {
    private FileConfiguration config = new YamlConfiguration();
    private File f = null;

    public ConfigManager() {
        load();
    }

    private void load() {
        try {
            Main plugin = Main.getInstance();
            f = new File(plugin.getDataFolder(), "config.yml");
            if (!f.exists()) {
                plugin.saveResource("config.yml", false);
            }
            config.load(new BufferedReader(new InputStreamReader(new FileInputStream(f), Charsets.UTF_8)));
            config.getConfigurationSection("Config").getKeys(false).forEach(world -> data.put(world, new TimeLimit(config.getString("Config." + world))));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Map<String, TimeLimit> data = new HashMap<>();

    public Map<String, TimeLimit> getData() {
        return data;
    }

    public List<String> getCommands() {
        return config.getStringList("Commands");
    }
}
